#include "stdafx.h"
#include "mystatusbar.h"
#include "MainFrm.h"

myStatusBar::myStatusBar(void)
{
}

myStatusBar::~myStatusBar(void)
{
}
BEGIN_MESSAGE_MAP(myStatusBar, CStatusBar)
	ON_WM_LBUTTONDOWN()
	//{{AFX_MSG_MAP(myStatusBar)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

bool PointInRect(CPoint& Pt, CRect& Rect)
{
	return Pt.x >= Rect.left
		&& Pt.x <= Rect.right
		&& Pt.y <= Rect.bottom
		&& Pt.y >= Rect.top;
}

void myStatusBar::OnLButtonDown(UINT nFlags, CPoint pt)
{
	ParentMainFrame *pFrame = (ParentMainFrame*) AfxGetMainWnd();
	myStatusBar	*StatusBar = &pFrame->m_wndStatusBar;
	CRect ItemRect;
	int Item = -1, Count;
	
	if (this != StatusBar)
		return;

	//1.Determine what kind of cell in the jabbing
	for (int i = 0 ; i < 3; i++)
	{
		GetItemRect(i+1, &ItemRect);
		if (PointInRect(pt, ItemRect))
		{
			Item = i;
			break;
		}
	}
	if ( Item == -1 )
		return;

	//2.Select importance, allocate memory, and change lock it and then pass on to the Clipboard
	Count = Itemtext[Item].GetLength()-StartNum[Item];
	CString str = Itemtext[Item].Mid(StartNum[Item], Count);
	HGLOBAL hString = ::GlobalAlloc(GMEM_MOVEABLE, sizeof(TCHAR) * (str.GetLength() + 1));
 
	if (hString)
		if (::OpenClipboard (AfxGetMainWnd()->m_hWnd))
		{
			LPVOID szString = ::GlobalLock (hString);
			if (szString)
			{
				memcpy (szString, (LPCTSTR)str, sizeof(TCHAR) * (str.GetLength() + 1));
				::GlobalUnlock (hString);
				::EmptyClipboard();
				::SetClipboardData(CF_TEXT, hString);
			}
			::CloseClipboard();
		}
	AfxMessageBox( "Copied value "+ str +" in a clipboard", MB_OK);
}

void myStatusBar::DrawItem(LPDRAWITEMSTRUCT lpDr)
{
	// TODO: Add your message handler code here and/or call default	
	CRect rect(&lpDr->rcItem);
	CString text="";
	if( lpDr->itemID-1 < Itemtext.GetSize() )
	{
		text=Itemtext[lpDr->itemID-1];
		SetTextColor(lpDr->hDC,TextColor[lpDr->itemID-1]);
	}
	SetBkMode(lpDr->hDC,TRANSPARENT);
	TextOut(lpDr->hDC,rect.left,rect.top,text,text.GetLength());				
}

CString myStatusBar::AddItemText(CString text, int NewTextColor)
{
	Itemtext.Add(text);
	TextColor.Add( NewTextColor );
	return text;
}

void myStatusBar::DestroyAllText(void)
{
	Itemtext.RemoveAll();
	TextColor.RemoveAll();
}
